
//新闻
new Vue({
    
    data:{

        query_info : {

            page : 1,

            per_page : 8,

            article_type_id : ''

        },                  //搜索参数

        sub_types: {},  //新闻分类

        articles:{},    //新闻相关信息   

        article_list : [], //新闻列表

        loading : false,

        show_list : true,   //是否显示列表，true显示，false显示详情

        news_detail : {},  //新闻详情

        page: 1,          //当前页数

        scroll_coord  : {

            X : 0,

            Y : 462,      //滚动条位置
        },

        scroll_back : 0,  //点击返回后还原新闻列表位置

    } ,

    mounted: function() {

        this.fetchNewsType()
            .then(this.hadAid);

    },
    methods:{

        /**
         * 格式化网址上面的参数
         * @param  {string} url {需要进行格式化的网址}
         * @return {Object} {格式化后的参数对象}
         */
        fomatDataFromUrl : function fomatDataFromUrl(url){

            var new_arr = {};

            var param_arr = url.split('?').pop();

            param_arr.split('&').forEach(function(param){

                new_arr[param.split('=')[0]] = param.split('=')[1];

            })

            return new_arr;

        },
        
        /**
         * 判断是否含有新闻分类id
         * @return {undefined}
         */
        hadAid : function hadAid(){

            var param = this.fomatDataFromUrl(location.href);

            var article_type_id = param.article_type_id;

            var detail_id = param.detail_id;

            var page = param.page;

            if( !article_type_id ) {

                this.changeTag(this.sub_types[0].id);

                return;
            }

            this.hadPage(article_type_id,page,detail_id);

        },

        /**
         * 是否含有页数的参数
         * @param  {string} id {article_type_id}
         * @return {undefined}
         */
        hadPage : function hadPage(id,page,detail_id){

            if( !page ) {

                this.changeTag(id)
                    .then(function(){
                        if( detail_id ) {
                            this.watchNewsDetail(detail_id);
                        }
                    })
            }else {

                this.query_info.article_type_id = id;

                this.changePage(page)
                    .then(function(){
                        if( detail_id ) {
                            this.watchNewsDetail(detail_id);
                        }
                    })

            }

        },
        
        /**
         * 获取新闻列表
         * @param  {string} page {当前页数}
         * @return {undefined}
         */
        fetchNewsList : function fetchNewsList(page){

            this.loading = true;

            var query_str = Qs.stringify(this.query_info);

            this.page = page || 1;

            return this.$http.get('/api/home/company-news?' + query_str)
                    
                    .then(function(res){
                                        
                        if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);
                        
                        this.articles = res.data.articles;
                        
                        this.article_list = res.data.articles.data;
                                        
                    })
                    .catch(function(error){

                        this.$Message.error(error.message || error.statusText);

                    })
                    .then(function(){

                        this.loading = false;
                        
                    })
        },

        /**
         * 获取新闻类型
         * @return {undefined}
         */
        fetchNewsType : function fetchNewsType() {

            this.loading = true;

            return this.$http.get('/api/home/company-news/create')

                            .then(function(res){

                                if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                                this.sub_types = res.data.sub_types;

                            })
                            .catch(function(error){

                                this.$Message.error(error.message);

                            })

                            .then(function() {

                                this.loading = false;

                            })

        },

        /**
         * 切换分类的方法
         * @param {String} id   新闻分类的id
         * @return {undefined}
         */
        changeTag : function changeTag(id) {

            if(this.loading) return;

            this.show_list = true;

            this.query_info.page = 1;

            this.query_info.article_type_id = id;

            return this.fetchNewsList()
                        .then(function(){
                            history.replaceState({},'','news?article_type_id=' + id);
                        })
        },


        /**
         * 切换分页的方法       
         * @param {Number} number  页数
         * @return {undefined}
         */
        changePage : function changePage(number){

            this.show_list = true;

            this.query_info.page = number;

            return this.fetchNewsList()
                        .then(function(){

                            history.replaceState({},'','news?article_type_id=' + this.query_info.article_type_id + '&page=' + number);

                            window.scrollTo(this.scroll_coord.X,this.scroll_coord.Y);

                        })

        },

        /**
         * 显示新闻详情
         * @param  {string} id {新闻对象id}
         * @return {undefined}
         */
        watchNewsDetail : function watchNewsDetail(id) {

            this.show_list = false;

            this.scroll_back = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

            history.replaceState({},'','news?article_type_id=' + this.query_info.article_type_id + '&page=' + this.query_info.page + '&detail_id=' + id);

            this.news_detail = this.article_list.filter(function(article){return parseInt(article.id) === parseInt(id)})[0];

            window.scrollTo(this.scroll_coord.X,this.scroll_coord.Y);
        },

        /**
         * 返回
         * @return {undefined}
         */
        goBack : function goBack() {

            this.show_list = true;

            history.replaceState({},'','news?article_type_id=' + this.query_info.article_type_id + '&page=' + this.query_info.page);

            if (this.article_list.length === 0) {

                this.fetchNewsList();

            } 

            this.$nextTick(function(){

                window.scrollTo(0,this.scroll_back);

            })

        },

    },

    components:{

        bymPagination: bymPagination

    }


}).$mount("#company_news");